<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="部门编码" prop="deptCode">
        <el-input
          v-model="queryParams.deptCode"
          placeholder="请输入部门编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部门状态" prop="dictEnableStatus">
        <el-select
          v-model="queryParams.dictEnableStatus"
          placeholder="请选择部门状态"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('dict_enable_status')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <!-- 表单 -->
    <div style="width: 100%; height: 50px; display: flex; justify-content: space-between">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['config:org-dept-archives:export']"
        >
          数据导出
        </el-button>
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['config:org-dept-archives:create']"
        >
          新增
        </el-button>
      </div>
      <div>
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      border
      @register="tableoperation"
      :tableHeight="tableHeight"
    >
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          @click="openForm('update', row.id)"
          v-hasPermi="['config:drawing-alter-type:update']"
        >
          编辑
        </el-button>
        <el-button
          link
          type="danger"
          @click="handleDelete(row.id)"
          v-hasPermi="['config:drawing-alter-type:delete']"
        >
          删除
        </el-button>
      </template>
    </Table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrgDeptArchivesForm ref="formRef" @success="getList" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getTableTitle" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
</template>

<script setup lang="ts" name="OrgDeptArchives">
   import { getIntDictOptions } from '@/utils/dict'
   import download from '@/utils/download'
   import * as OrgDeptArchivesApi from '@/api/config/orgDeptArchives'
   import OrgDeptArchivesForm from './OrgDeptArchivesForm.vue'
   import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
   const message = useMessage() // 消息弹窗
   const { t } = useI18n() // 国际化

   const queryParams = reactive({
     pageNo: 1,
     deptCode: null,
     deptName: null,
     dictEnableStatus: null
   })
   const queryFormRef = ref() // 搜索的表单
   const exportLoading = ref(false) // 导出的加载中

   const tableTitleList = ref<CrudSchema[]>([]) // 表头列表
   const code = 'OrgDeptArchives' //表头名

   // tableObject：表格的属性对象，可获得分页大小、条数等属性
   // tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
   const { tableObject, tableMethods } = useTable({
     getListApi: OrgDeptArchivesApi.getOrgDeptArchivesPage, // 分页接口
     defaultParams: queryParams
   })

   // 获得表格的各种操作
   const { getList, setSearchParams } = tableMethods
   /**表头内部方法 */
   const usTableoperation = ref()
   const tableoperation = (parent: any) => {
     usTableoperation.value = parent
   }
   // 获取表头
   const getTableTitle = async () => {
     const res = await OrgDeptArchivesApi.getOrgDeptArchivesPage(queryParams)
     console.log('use',res.list.length)
     res.labels.push({
       field: 'action',
       label: '操作',
       align: 'left',
       headerAlign: 'left',
       width: 110
     })
     const { allSchemas } = useCrudSchemas(res.labels)
     tableTitleList.value = allSchemas.tableColumns
   }

   // 打开表头设置
   const tableTitleSet = ref()
   const openTableTitleSet = () => {
     tableTitleSet.value.open(tableTitleList.value, code)
   }

   /** 搜索按钮操作 */
   const handleQuery = () => {
     queryParams.pageNo = 1
     setSearchParams(queryParams)
   }

   /** 重置按钮操作 */
   const resetQuery = () => {
     queryFormRef.value.resetFields()
     handleQuery()
   }

   /** 添加/修改操作 */
   const formRef = ref()
   const openForm = (type: string, id?: number) => {
     formRef.value.open(type, id)
   }

   /** 删除按钮操作 */
   const handleDelete = async (id: number) => {
     try {
       // 删除的二次确认
       await message.delConfirm()
       // 发起删除
       await OrgDeptArchivesApi.deleteOrgDeptArchives(id)
       message.success(t('common.delSuccess'))
       // 刷新列表
       await getList()
     } catch {}
   }

   /** 导出按钮操作 */
   const exportRef = ref()
   const handleExport = async () => {
     exportRef.value.open()
   }

   // 下载
   const downloadFun = async () => {
     
     const val = exportRef.value.params
     let params = {
       fileName: val.name,
       ids: ''
     }
     if (val.range === 'all') {
       params.ids = ''
     } else {
       params.ids = usTableoperation.value.selections.map((item) => item.id)
     }
     console.log(params)
     const data = await OrgDeptArchivesApi.exportOrgDeptArchives(params)
     download.excel(data, val.name)
     clearSelectData()
   }

   //清空多选
   const clearSelectData = () => {
     usTableoperation.value.selections = []
     usTableoperation.value.clearTableSelect()
   }

   /** 初始化 **/
   const tableHeight = ref(0)
   //132：表格内除表格以外的高度，127 顶部固定高度 + copyright高度
   onMounted(async () => {
     const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
     tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
     await getTableTitle()
     getList()
     
   })
</script>
<style lang="scss" scoped>
   .btnWrap {
     display: flex;
     justify-content: space-between;
     margin-bottom: 10px;
   }
</style>
